# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import time, datetime
import commonlib, re
import traceback
import workerpool
import threading
from MongoModel import MongoModel
from termcolor  import cprint
from CrawlerLib import Log, Http

#MONGO_SERVER = 'beta.mana.vn'
MONGO_SERVER = '27.0.12.106'
MONGO_PORT = 27017
DOWNLOAD_PATH = '/home/hoangnamhai/HarvestedData/tintuc/tym/bongda/'
PREFIX = '/uploads/bongda/'


class Bd7m(MongoModel):

    __listGiaiDau = {'92':'Anh', '34':'Ý', '39':'Đức', '93':'Pháp', '85':'Tây Ban Nha', '810': 'Việt Nam'}
    __listGiaiDau2 = {'74': 'Champions League', '58': 'Europa League'}
    __teamAmount = {}

    def __init__(self, host, port=27017):
        MongoModel.__init__(self, host, port)

    def standardlizeTimeValue(self, timeString):
        elems = timeString.split(',')
        for i in range(len(elems)):
            elems[i] = int(elems[i])
        myTime = datetime.datetime(elems[0], elems[1], elems[2], elems[3], elems[4], elems[5]) + datetime.timedelta(hours=-1)
        return myTime + datetime.timedelta(seconds=time.timezone)

    def generateSubstitutionEventText(self, playerTeam, playerIn, playerOut):
        return 'Đội {0} thay người: {1} vào sân thay cho {2}'.format(playerTeam, playerIn, playerOut)

    def generateEventText(self, playerName, playerTeam, eventType):
        templates = {0:'{0} ({1}) ghi bàn',
                     1:'{0} ({1}) ghi bàn từ quả đá phạt 11m',
                     2:'{0} ({1}) đá phản lưới nhà',
                     3:'{0} ({1}) nhận thẻ vàng',
                     4:'{0} ({1}) nhận thẻ đỏ trực tiếp',
                     5:'{0} ({1}) nhận thẻ vàng thứ hai',}
        return templates[eventType].format(playerName, playerTeam)

    def generateEventType(self, eventId):
        eventTypes = {0:"goal",
                      1:"11m",
                      2:"owngoal",
                      3:"yellow",
                      4:"red",
                      5:"yellow2",
                      6:"subs",}
        return eventTypes[eventId]

    def generateImageType(self, eventId):
        try:
            iconImg = ''
            '''
            eventTypes = {0:"http://www.gammamusic.com/copa2k6/Copa/copa_archivos/balongde.jpg",
                          1:"http://www.gammamusic.com/copa2k6/Copa/copa_archivos/balongde.jpg",
                          2:"http://bongdaso.com/img/nogoal.gif",
                          3:"http://bongdaso.com/img/yellow.gif",
                          4:"http://bongdaso.com/img/red.gif",
                          5:"http://bongdaso.com/img/2yellow.gif",
                          6:"http://bongdaso.com/img/subs2.gif",}
            '''
            eventTypes = {0:"/uploads/bongda/imgs/ball.jpg",
                          1:"/uploads/bongda/imgs/redball.jpg",
                          2:"/uploads/bongda/imgs/nogoal.jpg",
                          3:"/uploads/bongda/imgs/yellow.jpg",
                          4:"/uploads/bongda/imgs/red.jpg",
                          5:"/uploads/bongda/imgs/2yellow.jpg",
                          6:"/uploads/bongda/imgs/subs1.jpg",}
            #iconImg = commonlib.downloadImage('{0}/{1}'.format('http://ty.mana.vn/uploads/bongda', eventTypes[eventId]), DOWNLOAD_PATH, PREFIX)
            iconImg  = eventTypes[eventId]
            return iconImg
        except:
            return ''

    def summarizeMatch(self, matchId):
        logger.debug('start summarizeMatch(matchId={0})'.format(matchId))
        tomtat = ''
        hUrl = 'http://wlive.7m.cn/predictJs/vn/{0}.js'.format(matchId)
        aUrl = 'http://data.7m.cn/analyse/vn/predictions/{0}.js'.format(matchId)
        html = commonlib.getHTML(hUrl)
        des = commonlib.extractWithRegEx(r'desc":"(.+)"', html, 1)
        if (des != ''): tomtat += commonlib.encodeUTF8Str(des) + " \n"
        html = Http.getHtml(aUrl)
        des = commonlib.extractWithRegEx(r'var predictions_ct = \'(.+)\'', html, 1)
        if (des != ''): tomtat += commonlib.encodeUTF8Str(des)
        db = self.connection['bongda']
        collection = db['summarize']
        print tomtat
        if len(des) > 10:
            collection.save({
                '_id': matchId,
                'data': tomtat,
                'timestamp': time.time(),
                'lastupdate': datetime.datetime.now()
            })

    def match(self, matchId, maGiaidau=92, score='VS'):
        '''
        tom tat tran:
            http://wlive.7m.cn/predictJs/vn/{0}.js
            http://data.7m.cn/analyse/vn/predictions/{0}.js
        '''
        logger.debug('start match: {0}'.format(matchId))
        try:
            url = 'http://data2.7m.cn:13000/goaldata/vn/{0}.js?nocache={1}'.format(matchId, time.time())
            html = commonlib.replaceStr(r';', ';\n', Http.getHtml(url))
            d_pn, d_sx, d_tb, d_ta, d_tm, d_lx, d_bf = '', '', '', '', '', '', ''
            d_stm, d_ssx, d_dpn, d_upn = '', '', '', ''
            stat_arr = {3: ['shoots', 'Sút bóng'], 4: ['shootOnGoals', 'Sút cầu môn'], 5: ['fouls', 'Phạm lỗi'], 6: ['conerKicks', 'Phạt góc'], 9: ['offSides', 'Việt vị'], 11: ['yellowCards', 'Thẻ vàng'], 14: ['ballPossession', 'Giữ bóng'], 29: ['subSituation', 'Thay người'], 13: ['redCards', 'Thẻ đỏ']}
            matchStat = {}
            for k, v in stat_arr.items():
                try:
                    dd = commonlib.extractWithRegEx(r'd_live_stat\[{0}\] = \'(.+)\';'.format(k), html, 1)
                    if dd == '':
                        matchStat[v[0]] = {'h': 0, 'a': 0, 'name': v[1]}
                        continue
                    dd = dd.split(',')
                    matchStat[v[0]] = {'h': dd[0], 'a': dd[1], 'name': v[1]}
                except: pass
            for line in re.findall(r'.*var\s(.+);', html):
                try: 
                    exec(line)
                except: 
                    pass
            data = []
            for i in range(len(d_pn)):
                playerTeamIndex = d_sx[i]
                playerTeam = d_tb if playerTeamIndex < 0 else d_ta
                eventData = {}
                eventData['time'] = d_tm[i]
                eventData['text'] = self.generateEventText(d_pn[i], playerTeam, d_lx[i])
                eventData['type'] = self.generateEventType(d_lx[i])
                eventData['icon'] = self.generateImageType(d_lx[i])
                eventData['playerName'] = d_pn[i]
                eventData['home'] = 0 if playerTeamIndex < 0 else 1
                cprint(eventData['icon'], 'yellow')
                eventData['matchScore'] = d_bf[i]
                data.append(eventData)
            for i in range(len(d_stm)):
                playerTeamIndex = d_ssx[i]
                playerTeam = d_tb if playerTeamIndex < 0 else d_ta
                eventData = {}
                eventData['time'] = d_stm[i]
                eventData['text'] = self.generateSubstitutionEventText(playerTeam, d_upn[i], d_dpn[i])
                eventData['type'] = self.generateEventType(6)
                eventData['icon'] = self.generateImageType(6)
                eventData['playerName'] = ''
                eventData['home'] = 0 if playerTeamIndex < 0 else 1
                cprint(eventData['icon'], 'yellow')
                eventData['matchScore'] = ''
                data.append(eventData)
            data.sort(key=lambda m: m['time'])
            for i in data:
                print i['time'], ': ', i['text']
            result = {'_id': matchId,
                'data': data,
                'score': score,
                'lastupdate': datetime.datetime.now(),
                'timestamp': time.time(),
                'stat': matchStat,
                'homeTeam': d_ta,
                'awayTeam': d_tb,
                'maGiaidau': maGiaidau
                }
            db = self.connection['bongda']
            collection = db['matches']
            collection.save(result)
        except:
            print 'ERROR: ', matchId, '(giai dau: {0})'.format(maGiaidau)
            logger.error(traceback.format_exc())

    def topshooter(self, maGiaidau):
        logger.debug('start topshooter {0}'.format(maGiaidau))
        try:
            html = Http.getHtml('http://data.7m.cn/matches_data/{0}/vn/shooter.js?a={1}'.format(maGiaidau, time.time())).replace(';',';\n')
            lines = re.findall(r".*var\s(.+);", html)
            Shooters = ''
            for line in lines:
                exec(line)
            data = []
            soCauThu = len(Shooters)
            for i in range(soCauThu):
                row = {}
                row['_id'] = Shooters[i][2][0]
                row['name'] = commonlib.encodeUTF8Str(Shooters[i][2][1])
                row['rank'] = Shooters[i][0]
                row['teamName'] = commonlib.encodeUTF8Str(Shooters[i][1][1])
                row['teamId'] = Shooters[i][1][0]
                row['score'] = Shooters[i][3]
                data.append(row)
            result = {'_id': maGiaidau,
                'giaidau': self.__listGiaiDau[maGiaidau],
                'lastupdate': datetime.datetime.now(),
                'timestamp': time.time(),
                'data': data
                }
            db = self.connection['bongda']
            collection = db['topghiban']
            collection.save(result)
        except:
            logger.error(traceback.format_exc())

    def fixture(self, maGiaidau):
        logger.debug('start fixture: %s' % self.__listGiaiDau[maGiaidau])
        try:
            html = Http.getHtml('http://data.7m.cn/matches_data/{0}/vn/fixture.js?a={1}'.format(maGiaidau, time.time()))
            html = html.replace(';',';\n')
            lines = re.findall(r".*var\s(.+);", html)
            Tmp_bh_Arr, TeamA_bh, TeamB_bh, TeamA_Arr, TeamB_Arr, RedCardA_Arr, RedCardB_Arr, Run_Arr = '', '', '', '', '', '', '', ''
            Scores_Arr, Time_Arr, d_last_update, currentRun, Memo_Arr = '', '', '', '', []
            run_Head_Arr = []
            for line in lines:
                exec(line)
            soTranDau = len(Tmp_bh_Arr)
            soTranDauPerVong = len(Tmp_bh_Arr) / soTranDau
            print('Số trận đấu: {0}'.format(soTranDau))
            soVongDau = len(run_Head_Arr)
            data = []
            for i in range(soTranDau):
                row = {}
                row['_id'] = Tmp_bh_Arr[i]
                row['teamAId'] = TeamA_bh[i]
                row['teamAName'] = commonlib.encodeUTF8Str(TeamA_Arr[i])
                row['teamBId'] = TeamB_bh[i]
                row['teamBName'] = commonlib.encodeUTF8Str(TeamB_Arr[i])
                row['teamARedCard'] = RedCardA_Arr[i]
                row['teamBRedCard'] = RedCardB_Arr[i]
                row['run'] = Run_Arr[i]
                row['score'] = Scores_Arr[i]
                row['time'] =  self.standardlizeTimeValue(Time_Arr[i])
                row['memo'] = Memo_Arr[i]
                data.append(row)

            result = {'_id': maGiaidau,
                'giaidau': self.__listGiaiDau[maGiaidau],
                'lastupdate': datetime.datetime.now(),
                'timestamp': time.time(),
                'reporttime': commonlib.encodeUTF8Str(commonlib.replaceStr(r'\:\s', ':', d_last_update)),
                'data': data,
                'currentRound': currentRun,
                'numberOfRound': currentRun,
                'totalRounds': soVongDau,
                'type': 0,
                }
            db = self.connection['bongda']
            collection = db['bangkqtrandau']
            collection.save(result)
            for i in range(soTranDau):
                #if i > (currentRun + 1) * soTranDauPerVong: break
                matchId = Tmp_bh_Arr[i]
                teamA = commonlib.encodeUTF8Str(TeamA_Arr[i])
                teamB = commonlib.encodeUTF8Str(TeamB_Arr[i])
                score = Scores_Arr[i]
                # -----------------------------------------------------
                print('----------------------------------------------')
                print("%s - %s" % (teamA, teamB))
                print(score)
                # -----------------------------------------------------
                self.match(matchId, maGiaidau, score)
                self.summarizeMatch(matchId)
#                if score != 'VS' and i<=icm: self.match(matchId, maGiaidau, score)
#                if i >= icm and i <= icm + soTranDauPerVong: self.summarizeMatch(matchId) 
                
        except:
            logger.error(traceback.format_exc())

    def fixture2(self, giaidauId):
        url = 'http://data.7m.cn/matches_data/{0}/vn/matches.js?nocache={1}'.format(giaidauId, time.time())
        try:
            patterns = {'var\s*': '', '\[\]': '{}', 'new Array\(\)': '{}', ';': ';\n'}
            defaultord = 0; group_arr = []; RedCardA_arr = []; RedCardB_arr = []; score_arr = []; Start_time_arr = []
            live_bh_arr = []; TeamA_bh_arr = []; TeamA_arr = []; TeamB_bh_arr = []; TeamB_arr = []; groups_arr = []; s_name_arr = []
            d_last_update = ''; ord_arr = []; isGroups_arr = []; Statings_arr = []
            html = Http.getHtml(url)
            
            for i, v in patterns.items(): html = re.sub(i, v, html)
            for line in html.splitlines():
                exec(line)
            data = []
            listOfMatch = []
            for i in range(0, defaultord + 1):
                for j in range(len(live_bh_arr[i])):
                    match = {}
                    match['_id'] = live_bh_arr[i][j]
                    match['teamAId'] = TeamA_bh_arr[i][j]
                    match['teamAName'] = TeamA_arr[i][j]
                    match['teamBId'] = TeamB_bh_arr[i][j]
                    match['teamBName'] = TeamB_arr[i][j]
                    match['groupName'] = groups_arr[i][j]
                    match['groupRound'] = group_arr[i][j]
                    match['teamARedCard'] = RedCardA_arr[i][j]
                    match['teamBRedCard'] = RedCardB_arr[i][j]
                    match['score'] = score_arr[i][j]
                    match['time'] = self.standardlizeTimeValue(Start_time_arr[i][j])
                    match['roundName'] = s_name_arr[i]
                    match['run'] = i + 1
                    listOfMatch.append(match)
                    data.append(match)
            db = self.connection['bongda']
            collection = db['bangkqtrandau']
            collection.save({'_id': giaidauId,
                'giaidau': self.__listGiaiDau2[giaidauId],
                'lastupdate': datetime.datetime.now(),
                'timestamp': time.time(),
                'reporttime': d_last_update,
                'data': data,
                'currentRound': defaultord + 1,
                'currentRoundName': s_name_arr[defaultord],
                'numberOfRound': len(ord_arr),
                'type': 1,
                })
            ig = 0
            for i in range(len(isGroups_arr)):
                if isGroups_arr[i] == 1: ig = i; break
            collection = db['bangxephangUEFA']
            data = []
            for igroup in Statings_arr[ig].values():
                groupName = igroup[1][0]
                agroup = []
                for i in range(len(igroup[1])):
                    agroup.append({
                        'teamId': igroup[3][i],
                        'teamName': igroup[4][i],
                        'point': igroup[11][i],
                        'matchNum': igroup[5][i],
                        'win': igroup[6][i],
                        'draw': igroup[7][i],
                        'loss': igroup[8][i],
                        'get': igroup[9][i],
                        'miss': igroup[10][i],
                    })
                agroup.sort(key=lambda m: m['point'], reverse=True)
                data.append({'data': agroup, 'groupName': groupName})
            collection.save({'_id': giaidauId,
                'giaidau': self.__listGiaiDau2[giaidauId],
                'data': data,
                'timestamp': time.time(),
                'lastupdate': datetime.datetime.utcnow(),
                'reporttime': d_last_update
                })
            
            for match in listOfMatch:
                matchId = match['_id']
                # -----------------------------------------------------
                if match['run'] > defaultord + 1: break
                print('----------------------------------------------')
                print("%s - %s" % (match['teamAName'], match['teamBName']))
                print(match['score'])
                # -----------------------------------------------------
                self.match(matchId, giaidauId, match['score'])
                if match['run'] == defaultord + 1: self.summarizeMatch(matchId)
        except:
            logger.error(traceback.format_exc())

    def standing(self, maGiaidau):
        logger.debug('start standing: %s' % self.__listGiaiDau[maGiaidau])
        url = "http://data.7m.cn/matches_data/{0}/vn/standing.js?t={1}".format(maGiaidau, time.time())
        try:
            f_sds_ti, f_sds_tn, f_sds_mnum, f_sds_mw, f_sds_md, f_sds_ml = '', '', '', '', '', ''
            f_sds_mgs, f_sds_mga, f_sds_memo, f_sds_note, f_sds_pt, f_sds_deduction = '', '', '', '', '', ''
            h_sds_mw, h_sds_ml, h_sds_md, a_sds_mw, a_sds_ml, a_sds_md = '', '', '', '', '', ''
            sds_mn, sdss_last_update = '', ''
            html = commonlib.getHTML(url)
            html = Http.getHtml(url)
            for line in re.findall(r'.*var\s(.+);', html):
                exec(line)
            data = []
            self.__teamAmount[maGiaidau] = len(f_sds_ti)
            for i in range(len(f_sds_ti)):
                row = {}
                row['_id'] = f_sds_ti[i]
                row['thutu'] = i + 1
                row['name'] = commonlib.encodeUTF8Str(f_sds_tn[i])
                row['matchNum'] = f_sds_mnum[i]
                row['point'] = f_sds_pt[i]
                row['matchWin'] = f_sds_mw[i]
                row['matchDraw'] = f_sds_md[i]
                row['matchLose'] = f_sds_ml[i]
                row['homeWin'] = h_sds_mw[i]
                row['homeDraw'] = h_sds_md[i]
                row['homeLose'] = h_sds_ml[i]
                row['awayWin'] = a_sds_mw[i]
                row['awayDraw'] = a_sds_md[i]
                row['awayLose'] = a_sds_ml[i]
                row['goalget'] = f_sds_mgs[i]
                row['goalmiss'] = f_sds_mga[i]
                row['memo'] = commonlib.encodeUTF8Str(f_sds_memo[i])
                row['note'] = commonlib.encodeUTF8Str(f_sds_note[i])
                row['deduction'] = commonlib.encodeUTF8Str(f_sds_deduction[i])
                data.append(row)
            result = {'tenGiaiDau': commonlib.encodeUTF8Str(sds_mn),
                'giaidau': self.__listGiaiDau[maGiaidau],
                '_id': maGiaidau,
                'data': data,
                'reporttime': commonlib.encodeUTF8Str(commonlib.replaceStr(r'\:\s', ':', sdss_last_update)),
                'lastupdate': datetime.datetime.now(),
                'timestamp': time.time(),
                }
            db = self.connection['bongda']
            collection = db['bangxephang']
            collection.save(result)
        except:
            logger.error(traceback.format_exc())

    def process(self):
        try:
            pool = workerpool.WorkerPool(size=4)
            pool.map(self.topshooter, self.__listGiaiDau.keys())
            pool.map(self.standing, self.__listGiaiDau.keys())
            pool.map(self.fixture, self.__listGiaiDau.keys())
            pool.map(self.fixture2, self.__listGiaiDau2.keys())
            pool.shutdown()
            pool.wait()
        except:
            logger.error(traceback.format_exc())
        return
    

def quitIfTimeout():
    logger.debug('------------- check Timeout -------------')
    while True:
        delta = time.time() - lastaction
        if delta > 900:
            logger.info('process timeout {0}'.format(delta))
            os._exit
        time.sleep(10)

if __name__ == '__main__':
    logger = Log.getLogger('bd7mcn')
    logger.info('start crawler Bd7m')
    lastaction = time.time()
    threading.Thread(target=quitIfTimeout).start()
    reqHeader = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0'}
    bd = Bd7m(MONGO_SERVER, MONGO_PORT)
    #bd.match('473646')
#    bd.fixture('92')
    bd.process()
#    bd.tomTatTranDau(363905)
    logger.info('finished crawler Bd7m at {0}'.format(datetime.datetime.now()))
    os._exit(1)
#    os.kill(pid, 9)
